﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DatabaseAdminTools.Connections;

namespace DatabaseAdminTools.OptionForms
{
    public partial class UserManagerForm : Form
    {
        private Connections.Connection conn;
        private List<User> Users;
        private GlobalPrivs privControl;
        public static List<Form> CONNECTION_OPENED = new List<Form>();

        public UserManagerForm(Connection _conn)
        {
            InitializeComponent();
            conn = _conn;
            this.Text = "Users Manager - " + conn.Name;
            this.Name = conn.Name;
            CONNECTION_OPENED.Add(this);
        }

        public void refreshUserList()
        {
            treeView1.Nodes.Clear();
            Users = new List<User>();
            string sql = "SELECT user, host FROM mysql.user";
            try
            {
                DataTable usersData = conn.getTable(sql);
                if (usersData != null)
                {
                    foreach (DataRow row in usersData.Rows)
                    {
                        User user = new User(conn, row[0].ToString(), row[1].ToString());
                        Users.Add(user);
                        TreeNode node = new TreeNode(user.ToString(), 4, 4);
                        node.Name = user.ToString();
                        node.ContextMenuStrip = contextMenuStrip1;
                        treeView1.Nodes.Add(node);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this,"<refreshUserList>"+ ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Dispose();
            }
        }

        private bool OptionEnabled
        {
            set
            {
               toolStripButton2.Enabled = value;
               toolStripButton3.Enabled = value;
            }
        }

        private void UserManagerForm_Load(object sender, EventArgs e)
        {
            refreshUserList();
            privControl = new GlobalPrivs();
            privControl.Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(privControl);
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void newUserButton_Click(object sender, EventArgs e)
        {
            new UserPropertyForm(conn).ShowDialog(this);
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            TreeNode node = treeView1.SelectedNode;
            privControl.save();
            privControl.setData(Users[node.Index], node.Level);
            this.Cursor = Cursors.Default;
        }

        private void deleteUserButton_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node != null)
            {
                DatabaseAdminTools.MiniForms.ConfirmForm frm = new MiniForms.ConfirmForm("delete user", node.Name);
                frm.show(this, "Are you sure to DELETE User " + node.Name + " ?", "Delete User");
            }
        }

        public void deleteUser(string key)
        {
            try
            {
                int index = treeView1.Nodes[key].Index;
                Users[index].drop();
                Users.RemoveAt(index);
                treeView1.Nodes.RemoveAt(index);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void editUserButton_Click(object sender, EventArgs e)
        {
            if(treeView1.SelectedNode != null)
            {
                new UserPropertyForm(conn, Users[treeView1.SelectedNode.Index]).ShowDialog(this);
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            refreshUserList();
        }

        private void newUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            newUserButton_Click(sender, e);
        }

        private void deleteUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deleteUserButton_Click(sender, e);
        }

        private void editSelectedUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editUserButton_Click(sender, e);
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            refreshUserList();
        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            treeView1.SelectedNode = treeView1.GetNodeAt(e.Location);
        }
    }
}
